//called from dedupe trigger 
//called from OOR dupecheck - edited by IDC nov 2010
public without sharing class UpdateLeads {
       //@future (callout = true)   

      public static void doUpdate(Set<ID> leadBusNames, Set<string> bizNames, Set<ID> phoneNumLeads,Set<string> phones, ID LeadRecordType ,ID AccountRecordType,set<string> bizaccNames,set<string> oldphoneleads,set<string> oldbizNames){
           //reset the recursive flag   
           OORDuplicateCheck.setRecursive(true);      
                FindMatchingLeads( phones,  bizNames ,oldphoneleads,oldbizNames,LeadRecordType );
                FindInactiveOrProspAccwithPhoneMatch(phones,  phoneNumLeads, LeadRecordType,AccountRecordType);
                FindInactiveOrProspAccwithPrimPhoneMatch(phones,  phoneNumLeads, LeadRecordType,AccountRecordType);
                FindAccountswithNameMatch( bizaccNames ,leadBusNames, LeadRecordType , AccountRecordType);                 
           OORDuplicateCheck.setRecursive(false);
      }
      
      //find all matching leads
      private static void FindMatchingLeads(Set<string> phones, Set<string> bizNames ,Set<string> oldphoneleads,Set<string> oldbizNames,ID LeadRecordType )
      {
            FindLeadswithPhoneMatch(phones,LeadRecordType);
            FindLeadswithNameMatch(bizNames,LeadRecordType);
            FindLeadswithNameMatchOld(oldbizNames,LeadRecordType);
            FindLeadswithPhoneMatchOld(oldphoneleads,LeadRecordType);
      }
      
      //find leads with matching Phone
      private static void FindLeadswithPhoneMatch(Set<string> phones,ID LeadRecordType)
      {
            Map<String,List<Lead> > phoneLeadsMap  = new Map<String, List<Lead>>(); 
            system.debug('AAAAAAAAAAAaaFindMatchingLeads with phone'+phones);            
            List<Lead> dupingLeads =   [        SELECT ID, IsDupe__C,Master__c, DupingLeadID__c,duplicate__c,owner.name, Phone,CleanPhoneText__c, OwnerId 
                                                FROM     Lead 
                                                WHERE    cleanphonetext__C IN :phones and 
                                                         cleanphonetext__C != NULL and
                                                         RecordTypeid = :LeadRecordType and 
                                                         IsConverted = false 
                                                ORDER BY createddate ASC  ];
                 
            //create a  phone->leads map
            for(Lead lead : dupingLeads)    {               
               if(lead.cleanphonetext__C !=null && !phoneLeadsMap.containsKey(lead.cleanphonetext__C)) {
                  //add this new company list                 
                  phoneLeadsMap.put(lead.cleanphonetext__C, new List<Lead>{lead});
               }
               else {                
                 phoneLeadsMap.get(lead.cleanphonetext__C).add(lead);
               }
            } 
            
              DedupeLeads(phones, phoneLeadsMap,'Phone' );               
      }
    //Old leads with phone
     private static void FindLeadswithPhoneMatchOld(Set<string> phones,ID LeadRecordType)
      {
            Map<String,List<Lead> > phoneLeadsMap  = new Map<String, List<Lead>>(); 
            system.debug('AAAAAAAAAAAaaFindMatchingLeads with phone'+phones);            
            List<Lead> dupingLeads =   [        SELECT ID, IsDupe__C,Master__c, DupingLeadID__c,duplicate__c,owner.name, Phone,CleanPhoneText__c, OwnerId 
                                                FROM     Lead 
                                                WHERE    cleanphonetext__C IN :phones and 
                                                         cleanphonetext__C != NULL and 
                                                         RecordTypeid = :LeadRecordType and 
                                                         IsConverted = false 
                                                ORDER BY createddate ASC  ];
                 
            //create a  phone->leads map
            for(Lead lead : dupingLeads)    {               
               if(lead.cleanphonetext__C !=null && !phoneLeadsMap.containsKey(lead.cleanphonetext__C)) {
                  //add this new company list                 
                  phoneLeadsMap.put(lead.cleanphonetext__C, new List<Lead>{lead});
               }
               else {                
                 phoneLeadsMap.get(lead.cleanphonetext__C).add(lead);
               }
            } 
            
              DedupeOldGroupedLeads(phones, phoneLeadsMap,'Phone' );               
      }
      
      //mark leads as dupe
      private static void DedupeLeads(Set<string> items,Map<String,List<Lead>> itemMap,string dupeType )
      {
         //now mark all dup leads as duplicate
            List<Lead> batchOfLeads = new List<Lead>();
            system.debug('AAAAAAAAAAA3' + items.size());   
            for (string leadItem: items)    {
                //if thre are any duplicates only!                                                                                                                                 
                if (itemMap != null && itemMap.containsKey(leadItem) && itemMap.get(leadItem).size() > 1) {         
                        //take the current phone 
                        //update all phones ,first one as primary and rest all as duping leads
                         ID primaryDupingLead = null ;  
                         String primaryLeadOwner = null;
                         string uniquefield = null;  
                         //integer count = 0; 
                         for (Lead lead2update: itemMap.get(leadItem)){
                                     
                                     //update only if it is not already marked, we do not want to overwrite if it was already marked as phone duplicate!
                                     if(!lead2update.IsDupe__c)  {                             
                                            lead2update.IsDupe__C = true;
                                            lead2update.Duplicate_Status__C = 'Duplicate';
                                            lead2update.Duplicate_Reason_ID__c = (dupeType=='Phone' ? 1 : 2) ;                                            
                                            lead2update.Duplicate_Reason_Desc__c = dupeType;
                                            lead2update.duplicate__c = true;
                                            lead2update.Master__c=True;
                                              
                                            
                                      } //if            
                                                                                                                                         
                                     //mark the first lead as primary dupe!                           
                                      if(primaryDupingLead == null){
                                         primaryDupingLead = lead2update.Id;
                                         primaryLeadOwner   =  lead2update.owner.name;
                                         uniquefield        =  lead2update.id;
                                         lead2update.Unique_lead_list__c = lead2update.id;
                                       
                                      }                                                           
                                       else{
                                          if(primaryDupingLead != null)
                                               lead2update.DupingLeadID__c = primaryDupingLead ;
                                               lead2update.Duplicate_owner__c = primaryLeadOwner;
                                               lead2update.Unique_lead_list__c = uniquefield;
                                               lead2update.Master__c=False;
                                           
                                       }     
                                                                           
                                batchOfLeads.add(lead2update);
                               } //for           
                        } //if 
                } //for          
            try{
                update batchOfLeads;
            } catch (DMLException e){
                 //if there is an exception in update, notify developer :)
                 //next version shall log this into custom object and also send notification.
                 sendMessage(e.getMessage());                           
            }
            
            
      }
      //New Update method to Update the old grouped Leads
      private static void DedupeOldGroupedLeads(Set<string> items,Map<String,List<Lead>> itemMap,string dupeType )
      {
            List<Lead> batchOfLeads = new List<Lead>();
            system.debug('@@@@@@@@@@@@@@@@' + items.size());   
            for (string leadItem: items)    {
                //if thre are any duplicates only!                                                                                                                                 
                if (itemMap != null && itemMap.containsKey(leadItem) && itemMap.get(leadItem).size() > 0) {         
                        //take the current phone 
                        //update all phones ,first one as primary and rest all as duping leads
                         ID primaryDupingLead = null ;  
                         String primaryLeadOwner = null;
                         string uniquefield = null;  
                         integer count = 0; 
                         for (Lead lead2update: itemMap.get(leadItem)){
                                    if(itemMap.get(leadItem).size()==1){
                                            lead2update.IsDupe__C = false;
                                            lead2update.Duplicate_Status__C = 'Unique';
                                            lead2update.Duplicate_Reason_ID__c = 0 ;                                            
                                            lead2update.Duplicate_Reason_Desc__c = '';
                                            lead2update.duplicate__c = false;

                                         }                                                                                                                                         
                                     //mark the first lead as primary dupe!                           
                                      if(primaryDupingLead == null){
                                         primaryDupingLead = lead2update.Id;
                                         primaryLeadOwner   =  lead2update.owner.name;
                                         uniquefield        =  lead2update.id;
                                         lead2update.Master__c = True;
                                         lead2update.Unique_lead_list__c = lead2update.id;
                                         lead2update.Duplicate_owner__c = null;
                                         lead2update.DupingLeadID__c = null;
                                         //lead2update.Unique_lead_list__c = uniquefield;
                                       
                                      }                                                           
                                       else{
                                          if(primaryDupingLead != null)
                                               lead2update.DupingLeadID__c = primaryDupingLead ;
                                               lead2update.Duplicate_owner__c = primaryLeadOwner;
                                               lead2update.Unique_lead_list__c = uniquefield;
                                               lead2update.Master__c=False;
                                           
                                       }     
                                                                           
                                batchOfLeads.add(lead2update);
                                //count = count+1;
                               } //for           
                        } //if 
                } //for          
            try{
                system.debug('@@@'+batchOfLeads);
                update batchOfLeads;
            } catch (DMLException e){
                 //if there is an exception in update, notify developer :)
                 //next version shall log this into custom object and also send notification.
                 system.debug('WWWWW'+e.getMessage());                           
            }
            
            
      }
      
      
      public static void sendMessage(String message){
                 Messaging.SingleEmailMessage mail =new Messaging.SingleEmailMessage();
                 String[] toAddresses = new String[] {'smuraleedharan@attinteractive.com'};
                 mail.setToAddresses(toAddresses);
                 mail.setReplyTo('noreply@salesforce.com');
                 mail.setSenderDisplayName('Apex error message');
                 mail.setSubject('Error from Org : ' + UserInfo.getOrganizationName());
                 mail.setPlainTextBody('FindLeadswithNameMatch ' + message);
                 Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
      }
      //find leads with matching Name
      private static void FindLeadswithNameMatch(Set<string> leadBusNames,ID LeadRecordType)
      {
            Map<String,List<Lead> > businessLeadsMap  = new Map<String, List<Lead>>();          
            List<Lead> dupingLeads =   [        SELECT ID, IsDupe__C, DupingLeadID__c,owner.name,Company, OwnerId 
                                                FROM     Lead 
                                                WHERE    Company IN :leadBusNames and 
                                                         RecordTypeid = :LeadRecordType and 
                                                         IsConverted = false                                                         
                                                ORDER BY createddate ASC  ];
                 
            //create a  business  name->leads map
            for(Lead lead : dupingLeads)    {                               
                   if(lead.Company != null && !businessLeadsMap.containsKey(lead.Company.toLowerCase())) {
                      //add this new company list                 
                          businessLeadsMap.put(lead.Company.toLowerCase(), new List<Lead>{lead});
                   }
                   else {                  
                          businessLeadsMap.get(lead.Company.toLowerCase()).add(lead);
                   }                
            }
            
            DedupeLeads(leadBusNames, businessLeadsMap,'Name' );            
      }
      //For Old Map of leads to group with a new Master
      private static void FindLeadswithNameMatchOld(Set<string> leadBusNames,ID LeadRecordType)
      {
            Map<String,List<Lead> > businessLeadsMap  = new Map<String, List<Lead>>();          
            List<Lead> dupingLeads =   [        SELECT ID, IsDupe__C, DupingLeadID__c,owner.name,Company, OwnerId 
                                                FROM     Lead 
                                                WHERE    Company IN :leadBusNames and 
                                                         RecordTypeid = :LeadRecordType and 
                                                         IsConverted = false                                                         
                                                ORDER BY createddate ASC  ];
                 
            //create a  business  name->leads map
            for(Lead lead : dupingLeads)    {                               
                   if(lead.Company != null && !businessLeadsMap.containsKey(lead.Company.toLowerCase())) {
                      //add this new company list                 
                          businessLeadsMap.put(lead.Company.toLowerCase(), new List<Lead>{lead});
                   }
                   else {                  
                          businessLeadsMap.get(lead.Company.toLowerCase()).add(lead);
                   }                
            }
            system.debug('EEEE'+businessLeadsMap);
            DedupeOldGroupedLeads(leadBusNames, businessLeadsMap,'Name' );            
      }

      private static void FindInactiveOrProspAccwithPhoneMatch(Set<string> phones,Set<ID>PhoneNumLeads, ID LeadRecordType, ID AccountRecordType)
      {
            Map<String,List<Account> > phoneAccMap = new Map<String, List<Account>>(); 
            system.debug('AAAAAAAAAAAaaFindMatchingLeads with phone'+phones); 
            List<Lead> LeadPhoneList = [Select id,Master__c, name,Duplicate_Owner__c,DupingAccountID__c,Duplicate__c,IsDupe__C,Duplicate_Status__C,Duplicate_Reason_ID__c,Duplicate_Reason_Desc__c, phone, cleanphonetext__c, ownerid, owner.name from lead where id in:PhoneNumLeads and IsDupe__C = false and IsConverted =false and recordtypeid = :LeadRecordType];           
            List<Account> dupingAccs =   [        SELECT ID, owner.name, Phone,CleanPhoneText__c, OwnerId 
                                                FROM     Account
                                                WHERE    cleanphonetext__C IN :phones  and 
                                                         cleanphonetext__C != NULL and
                                                         RecordTypeid = :AccountRecordType and 
                                                        (Account_Status__C = 'Inactive' or Account_Status__C = 'Prospect')                                                          
                                                ORDER BY createddate ASC  ];
                 
            //create a  phone->leads map
            for(Account acc : dupingAccs)    {               
               if(acc.cleanphonetext__C !=null && !phoneAccMap.containsKey(acc.cleanphonetext__C)) {
                  //add this new company list                 
                  phoneAccMap.put(acc.cleanphonetext__C, new List<account>{acc});
               }
               else {                
                 phoneAccMap.get(acc.cleanphonetext__C).add(acc);
               }

            } 
            
            
            List<Lead> batchOfLeads = new List<Lead>();
            //go thru all dupe leads and mark as duplicate ,update the duping acc id
            if(phoneAccMap!=null && phoneAccMap.size() > 0  ){
                for (Lead newLeadCompany: LeadPhoneList )     {                       
                    if (phoneAccMap.containsKey(newLeadCompany.cleanphonetext__C)){          
                     for (Account dupingAcc: phoneAccMap.get(newLeadCompany.cleanphonetext__C))  {                                                                                 
                                //mark the first lead as duplicate
                                if(!newLeadCompany.IsDupe__C)   {                                                            
                                    newLeadCompany.IsDupe__C = true;
                                    newLeadCompany.Duplicate_Status__C = 'Duplicate';
                                    newLeadCompany.Duplicate_Reason_ID__c = 2 ;
                                    newLeadCompany.Duplicate_Reason_Desc__c = 'Phone';
                                    newLeadCompany.Duplicate__c = true;
                                    newLeadCompany.Master__c = false;
                                    
                                }                                                                                                                                                                    
                                 newLeadCompany.DupingAccountID__c = dupingAcc.ID ;
                                 newLeadCompany.Duplicate_Owner__c = dupingAcc.Owner.Name ;

                                 //newLeadCompany.OwnerId = MarketGroupQueueId;
                                 batchOfLeads.add(newLeadCompany);
                                 //assuming only one account will have this name
                                 break;                                  
                                                                                                
                        } 
                    }       
                    }
             //if there is an exception in update, notify developer :)
             //next version shall log this into custom object and also send notification.
            try{
                update batchOfLeads;
            } catch (DMLException e){   
                
                /* by venkat        
                 Messaging.SingleEmailMessage mail =new Messaging.SingleEmailMessage();
                 String[] toAddresses = new String[] {'smuraleedharan@attinteractive.com'};
                 mail.setToAddresses(toAddresses);
                 mail.setReplyTo('noreply@salesforce.com');
                 mail.setSenderDisplayName('Apex error message');
                 mail.setSubject('Error from Org : ' + UserInfo.getOrganizationName());
                 mail.setPlainTextBody('FindAccountswithNameMatch '  + e.getMessage());
                 Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
                 */
                 //by venkat
                 sendMessage(e.getMessage());
            }
            
                                    
            }          
      }
      private static void FindInactiveOrProspAccwithPrimPhoneMatch(Set<string> phones,Set<ID>PhoneNumLeads, ID LeadRecordType, ID AccountRecordType)
      {
            Map<String,List<Account> > phoneAccMap = new Map<String, List<Account>>(); 
            system.debug('AAAAAAAAAAAaaFindMatchingLeads with phone'+phones); 
            List<Lead> LeadPhoneList = [Select id, name,Duplicate_Owner__c,DupingAccountID__c,Duplicate__c,IsDupe__C,Duplicate_Status__C,Duplicate_Reason_ID__c,Duplicate_Reason_Desc__c, phone, cleanphonetext__c, ownerid, owner.name from lead where id in:PhoneNumLeads and IsDupe__C = false and IsConverted =false and recordtypeid = :LeadRecordType];           
            List<Account> dupingAccs =   [        SELECT ID, owner.name, Phone,CleanPrimaryPhone_text__c, OwnerId 
                                                FROM     Account
                                                WHERE    CleanPrimaryPhone_text__c IN :phones  and 
                                                         RecordTypeid = :AccountRecordType and 
                                                        (Account_Status__C = 'Inactive' or Account_Status__C = 'Prospect')                                                          
                                                ORDER BY createddate ASC  ];
                 
            //create a  phone->leads map
            for(Account acc : dupingAccs)    {               
               if(acc.CleanPrimaryPhone_text__c !=null && !phoneAccMap.containsKey(acc.CleanPrimaryPhone_text__c)) {
                  //add this new company list                 
                  phoneAccMap.put(acc.CleanPrimaryPhone_text__c, new List<account>{acc});
               }
               else {                
                 phoneAccMap.get(acc.CleanPrimaryPhone_text__c).add(acc);
               }

            } 
            
            
            List<Lead> batchOfLeads = new List<Lead>();
            //go thru all dupe leads and mark as duplicate ,update the duping acc id
            if(phoneAccMap!=null && phoneAccMap.size() > 0  ){
                for (Lead newLeadCompany: LeadPhoneList )     {                       
                    if (phoneAccMap.containsKey(newLeadCompany.cleanphonetext__C)){          
                     for (Account dupingAcc: phoneAccMap.get(newLeadCompany.cleanphonetext__C))  {                                                                                 
                                //mark the first lead as duplicate
                                if(!newLeadCompany.IsDupe__C)   {                                                            
                                    newLeadCompany.IsDupe__C = true;
                                    newLeadCompany.Duplicate_Status__C = 'Duplicate';
                                    newLeadCompany.Duplicate_Reason_ID__c = 2 ;
                                    newLeadCompany.Duplicate_Reason_Desc__c = 'Phone';
                                    newLeadCompany.Duplicate__c = true;
                                    
                                }                                                                                                                                                                    
                                 newLeadCompany.DupingAccountID__c = dupingAcc.ID ;
                                 newLeadCompany.Duplicate_Owner__c = dupingAcc.Owner.Name ;

                                 //newLeadCompany.OwnerId = MarketGroupQueueId;
                                 batchOfLeads.add(newLeadCompany);
                                 //assuming only one account will have this name
                                 break;                                  
                                                                                                
                        } 
                    }       
                    }
             //if there is an exception in update, notify developer :)
             //next version shall log this into custom object and also send notification.
            try{
                update batchOfLeads;
            } catch (DMLException e){ 
                /* by Venkat          
                 Messaging.SingleEmailMessage mail =new Messaging.SingleEmailMessage();
                 String[] toAddresses = new String[] {'smuraleedharan@attinteractive.com'};
                 mail.setToAddresses(toAddresses);
                 mail.setReplyTo('noreply@salesforce.com');
                 mail.setSenderDisplayName('Apex error message');
                 mail.setSubject('Error from Org : ' + UserInfo.getOrganizationName());
                 mail.setPlainTextBody('FindAccountswithNameMatch '  + e.getMessage());
                 Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
                 */
                 sendMessage(e.getMessage());
            }
            
                                    
            }          
      }  
      //find accounts with matching Name
      private static void FindAccountswithNameMatch(Set<string> leadBusNames ,Set<ID> leadBusIds,ID LeadRecordType ,ID AccountRecordType)
        {
            //get the newly inserted records..
            List<Lead> leadBusinessNameList =[ SELECT ID, IsDupe__C,OOR_LeadCityState__c, DupingLeadID__c,Duplicate__c, DupingAccountID__c, Company, OwnerId FROM Lead WHERE ID IN :leadBusIds and RecordTypeid = :LeadRecordType and IsDupe__C =FALSE and IsConverted =false order by createddate asc  ];
             
            Map<String,List<Account> > businessAccMap  = new Map<String, List<Account>>();
            
            //get the Accounts with same biz names 
            List<Account> dupingAccounts = [ SELECT ID,Name,Owner.Name,OOR_AccountCityState__c FROM Account WHERE OOR_AccountCityState__c IN :leadBusNames and RecordTypeid = :AccountRecordType];        
            //create a  business  name->leads map
            for(Account acc : dupingAccounts)       {
                if(acc.Name != null){                                      
                       if(!businessAccMap.containsKey(acc.Name.toLowerCase())) {
                          //add this new company list                         
                          businessAccMap.put(acc.OOR_AccountCityState__c.toLowerCase() , new List<Account>{acc});
                       }
                       else {                                     
                          businessAccMap.get(acc.OOR_AccountCityState__c.toLowerCase()).add(acc);
                       }
                }
            }
            
            List<Lead> batchOfLeads = new List<Lead>();
            //go thru all dupe leads and mark as duplicate ,update the duping acc id
            if(businessAccMap !=null && businessAccMap.size() > 0  ){
                for (Lead newLeadCompany: leadBusinessNameList)     {                       
                    if (businessAccMap.containsKey(newLeadCompany.OOR_LeadCityState__c.toLowerCase())){          
                     for (Account dupingAcc: businessAccMap.get(newLeadCompany.OOR_LeadCityState__c.toLowerCase()))  {                                                                                 
                                //mark the first lead as duplicate
                                if(!newLeadCompany.IsDupe__C)   {                                                            
                                    newLeadCompany.IsDupe__C = true;
                                    newLeadCompany.Duplicate_Status__C = 'Duplicate';
                                    newLeadCompany.Duplicate_Reason_ID__c = 2 ;
                                    newLeadCompany.Duplicate_Reason_Desc__c = 'Name';
                                    newLeadCompany.Duplicate__c = true;
                                    //newLeadCompany.Master__c = false;
                                }                                                                                                                                                                    
                                 newLeadCompany.DupingAccountID__c = dupingAcc.ID ;
                                 newLeadCompany.Duplicate_Owner__c = dupingAcc.Owner.Name ;

                                 //newLeadCompany.OwnerId = MarketGroupQueueId;
                                 batchOfLeads.add(newLeadCompany);
                                 //assuming only one account will have this name
                                 break;                                  
                                                                                                
                        } 
                    }       
                    }
             //if there is an exception in update, notify developer :)
             //next version shall log this into custom object and also send notification.
            try{
                update batchOfLeads;
            } catch (DMLException e){     
                /* by venkat      
                 Messaging.SingleEmailMessage mail =new Messaging.SingleEmailMessage();
                 String[] toAddresses = new String[] {'smuraleedharan@attinteractive.com'};
                 mail.setToAddresses(toAddresses);
                 mail.setReplyTo('noreply@salesforce.com');
                 mail.setSenderDisplayName('Apex error message');
                 mail.setSubject('Error from Org : ' + UserInfo.getOrganizationName());
                 mail.setPlainTextBody('FindAccountswithNameMatch '  + e.getMessage());
                 Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
                 */
                 sendMessage(e.getMessage());
                 
            }
            
                                    
            } 
   } 
   
    //test method 
    static testMethod void testUpLead(){
        try{
            //doUpdate(Set<ID> leadBusNames, Set<string> bizNames, Set<ID> phoneNumLeads,Set<string> phones, ID LeadRecordType ,ID AccountRecordType){
            String accid = SilCustomerHelper.GetLocalAdvertiserAccountRecordType();
            Account acc = new Account(name='Test1',CleanPrimaryPhone_text__c='8989898950',
                                phone='8989898950',cleanphonetext__c='8989898950',
                                RecordTypeid=accid,Account_Status__C='Inactive');
                
            insert acc;
            
            Account acc1 = new Account(name='Test1',CleanPrimaryPhone_text__c='8989898951',
                                phone='8989898951',cleanphonetext__c='8989898951',
                                RecordTypeid=accid,Account_Status__C='Inactive');
                
            insert acc1;
            
            ID localLeadRecID = RecordTypeHelper.GetRecordTypeID('Lead', 'Local Advertiser'); 
            Lead ld = new Lead(RecordTypeId=localLeadRecID,Company ='Test',
                                FirstName='John',LastName='Smith',Phone='8989898950',
                                Disposition__c='Ready for Order',IsDupe__C=false,Duplicate_Status__C='Duplicate',
                                Duplicate_Reason_ID__c=2,Duplicate_Reason_Desc__c = 'Name',cleanphonetext__C='8989898950',
                                Duplicate__c=true,IsConverted=false
                                );
            insert ld;

            Lead ld1 = new Lead(RecordTypeId=localLeadRecID,Company ='Test1',
                                FirstName='John',LastName='Smith',Phone='8989898951',
                                Disposition__c='Ready for Order',IsDupe__C=false,Duplicate_Status__C='Duplicate',
                                Duplicate_Reason_ID__c=2,Duplicate_Reason_Desc__c = 'Name',cleanphonetext__C='8989898951',
                                Duplicate__c=true,IsConverted=false
                                );
            insert ld1;

            //System.assert(false,ld);
            Set<Id> leadBusNames = new Set<ID>();
            Set<String>  bizNames = new Set<String>();
            leadBusNames.add(ld.Id); 
            leadBusNames.add(ld1.Id); 
            bizNames.add(ld.company);
            bizNames.add(ld1.company);
            
            
            Set<Id> phoneNumLeads = new Set<ID>();
            Set<String> phones = new Set<String>();
            phoneNumLeads.add(acc.Id);
            phoneNumLeads.add(acc1.Id);
            phones.add(acc.cleanphonetext__c);
            phones.add(acc1.cleanphonetext__c);
            
            UpdateLeads.sendMessage('Testing Error');
            
            try{
                UpdateLeads.doUpdate(leadBusNames,bizNames,phoneNumLeads,phones,localLeadRecID,accid,null,null,null);
            }catch(Exception e1){}
            try{
                UpdateLeads.doUpdate(leadBusNames,bizNames,phoneNumLeads,phones,null,null,null,null,null);
            }catch(Exception e2){}
            try{
                UpdateLeads.doUpdate(null,null,phoneNumLeads,phones,null,null,null,null,null);
            }catch(Exception e3){}
            try{
                UpdateLeads.doUpdate(null,null,null,null,null,null,null,null,null);
            }catch(Exception e){}
           
           //System.assert(false,'>>>'+leads);                  
        //leadBusNames, bizNames, phoneNumLeads, phones, LeadRecordType, accid);
        }catch(Exception e){
            System.debug('Error: '+ e);
        }
    } 

  
}